toolchain("crossclang")
	set_kind("standalone")
	set_toolset("cc", "clang")
	set_toolset("cxx", "clang++")
	set_toolset("ar", "llvm-ar")
	set_toolset("as", "clang")
	set_toolset("nm", "llvm-nm")
	set_toolset("ranlib", "llvm-ranlib")
	set_toolset("strip", "llvm-strip")
	set_toolset("ld", "clang++", "clang")
	set_toolset("sh", "clang++", "clang")

	set_runtimes("c++_static", "c++_shared", "stdc++_static", "stdc++_shared")

	on_check(function (toolchain)
		import("core.project.config")
		import("detect.sdks.find_cross_toolchain")
		local sdkdir = toolchain:sdkdir()
    	local bindir = toolchain:bindir()

		if is_plat("cross") then
			local cross_toolchain = find_cross_toolchain(sdkdir, {bindir = bindir})
			if cross_toolchain then
				toolchain:config_set("bindir", cross_toolchain.bindir)
				toolchain:config_set("sdkdir", cross_toolchain.sdkdir)
				toolchain:config_set("cross", cross_toolchain.cross)
				toolchain:configs_save()
				-- init default target os
				if not config.get("target_os") then
					config.set("target_os", "linux", {readonly = true, force = true})
				end
			end
		end

        return import("lib.detect.find_tool")("clang")
    end)

	on_load(function (toolchain)
		if toolchain:config("cross") then
			local cc = vformat("%s/%sgcc",toolchain:config("bindir"),toolchain:config("cross"))

			local target, errdata = os.iorun("%s -dumpmachine", cc)
			if not target then
				raise("Can not find target")
			end
			toolchain:add("cxflags", "--target=" .. target, "-march=armv8.5a")
			toolchain:add("asflags", "--target=" .. target, "-march=armv8.5a")
			toolchain:add("ldflags", "--target=" .. target, "-march=armv8.5a")
			toolchain:add("shflags", "--target=" .. target, "-march=armv8.5a")

			local sysroot, errdata = os.iorun("%s -print-sysroot", cc)
			if not sysroot then
				raise("Can not find sysroot")
			end
			toolchain:add("cxflags", "--sysroot=" .. sysroot, "--gcc-toolchain=" .. toolchain:config("sdkdir"))
			toolchain:add("asflags", "--sysroot=" .. sysroot, "--gcc-toolchain=" .. toolchain:config("sdkdir"))
			toolchain:add("ldflags", "--sysroot=" .. sysroot, "--gcc-toolchain=" .. toolchain:config("sdkdir"))
			toolchain:add("shflags", "--sysroot=" .. sysroot, "--gcc-toolchain=" .. toolchain:config("sdkdir"))
		end
		toolchain:add("ldflags", "-fuse-ld=lld")
		toolchain:add("shflags", "-fuse-ld=lld")
		-- toolchain:add("ldflags", "--rtlib=libgcc")
    end)